// user-setting.js
const app = getApp();

Page({
  data: {
    userInfo: {},
    notifications: true,
    autoLogin: true,
    darkMode: false,
    cacheSize: '0KB',
    appVersion: '1.0.0'
  },

  onLoad: function() {
    // 加载用户信息
    this.loadUserInfo();
    
    // 加载设置
    this.loadSettings();
    
    // 计算缓存大小
    this.calculateCacheSize();
  },

  // 加载用户信息
  loadUserInfo: function() {
    const userInfo = wx.getStorageSync('userInfo') || {};
    this.setData({
      userInfo: userInfo
    });
  },

  // 加载设置
  loadSettings: function() {
    const notifications = wx.getStorageSync('notifications') !== false; // 默认开启
    const autoLogin = wx.getStorageSync('autoLogin') !== false; // 默认开启
    const darkMode = wx.getStorageSync('darkMode') === true; // 默认关闭
    
    this.setData({
      notifications: notifications,
      autoLogin: autoLogin,
      darkMode: darkMode
    });
  },

  // 计算缓存大小
  calculateCacheSize: function() {
    // 模拟缓存大小
    this.setData({
      cacheSize: '2.5MB'
    });
  },

  // 修改头像
  changeAvatar: function() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0];
        
        wx.showLoading({
          title: '上传中...',
        });
        
        // 模拟上传头像
        setTimeout(() => {
          wx.hideLoading();
          
          // 更新用户信息
          let userInfo = this.data.userInfo;
          userInfo.avatarUrl = tempFilePath;
          
          wx.setStorageSync('userInfo', userInfo);
          
          this.setData({
            userInfo: userInfo
          });
          
          wx.showToast({
            title: '头像更新成功',
            icon: 'success'
          });
        }, 1500);
      }
    });
  },

  // 修改昵称
  changeNickname: function() {
    wx.showModal({
      title: '修改昵称',
      editable: true,
      placeholderText: '请输入新昵称',
      defaultValue: this.data.userInfo.nickName || '',
      success: (res) => {
        if (res.confirm && res.content) {
          let userInfo = this.data.userInfo;
          userInfo.nickName = res.content;
          
          wx.setStorageSync('userInfo', userInfo);
          
          this.setData({
            userInfo: userInfo
          });
          
          wx.showToast({
            title: '昵称更新成功',
            icon: 'success'
          });
        }
      }
    });
  },

  // 修改手机号
  changePhone: function() {
    wx.showModal({
      title: '修改手机号',
      content: '请使用验证码方式修改手机号',
      confirmText: '获取验证码',
      success: (res) => {
        if (res.confirm) {
          // 这里应该跳转到验证码页面
          wx.showToast({
            title: '验证码功能开发中',
            icon: 'none'
          });
        }
      }
    });
  },

  // 修改密码
  changePassword: function() {
    wx.navigateTo({
      url: '/pages/change-password/change-password'
    });
  },

  // 消息通知开关
  onNotificationsChange: function(e) {
    const value = e.detail.value;
    
    wx.setStorageSync('notifications', value);
    
    this.setData({
      notifications: value
    });
    
    wx.showToast({
      title: value ? '消息通知已开启' : '消息通知已关闭',
      icon: 'success'
    });
  },

  // 自动登录开关
  onAutoLoginChange: function(e) {
    const value = e.detail.value;
    
    wx.setStorageSync('autoLogin', value);
    
    this.setData({
      autoLogin: value
    });
    
    wx.showToast({
      title: value ? '自动登录已开启' : '自动登录已关闭',
      icon: 'success'
    });
  },

  // 深色模式开关
  onDarkModeChange: function(e) {
    const value = e.detail.value;
    
    wx.setStorageSync('darkMode', value);
    
    this.setData({
      darkMode: value
    });
    
    wx.showToast({
      title: value ? '深色模式已开启' : '深色模式已关闭',
      icon: 'success'
    });
  },

  // 清理缓存
  clearCache: function() {
    wx.showModal({
      title: '清理缓存',
      content: '确定要清理所有缓存吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '清理中...',
          });
          
          // 模拟清理缓存
          setTimeout(() => {
            wx.hideLoading();
            
            this.setData({
              cacheSize: '0KB'
            });
            
            wx.showToast({
              title: '缓存清理成功',
              icon: 'success'
            });
          }, 1500);
        }
      }
    });
  },

  // 导出数据
  exportData: function() {
    wx.showModal({
      title: '导出数据',
      content: '确定要导出您的数据吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '导出中...',
          });
          
          // 模拟导出数据
          setTimeout(() => {
            wx.hideLoading();
            
            wx.showToast({
              title: '数据导出成功',
              icon: 'success'
            });
          }, 2000);
        }
      }
    });
  },

  // 隐私设置
  privacySettings: function() {
    wx.navigateTo({
      url: '/pages/privacy-settings/privacy-settings'
    });
  },

  // 检查更新
  checkUpdate: function() {
    wx.showLoading({
      title: '检查更新中...',
    });
    
    // 模拟检查更新
    setTimeout(() => {
      wx.hideLoading();
      
      wx.showModal({
        title: '检查更新',
        content: '当前已是最新版本',
        showCancel: false
      });
    }, 1500);
  },

  // 用户协议
  userAgreement: function() {
    wx.navigateTo({
      url: '/pages/user-agreement/user-agreement'
    });
  },

  // 隐私政策
  privacyPolicy: function() {
    wx.navigateTo({
      url: '/pages/privacy-policy/privacy-policy'
    });
  },

  // 联系我们
  contactUs: function() {
    wx.showModal({
      title: '联系我们',
      content: '客服电话：400-123-4567\n工作时间：9:00-18:00',
      showCancel: false
    });
  },

  // 退出登录
  logout: function() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除登录状态
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync('token');
          
          // 如果关闭了自动登录，清除记住的密码
          if (!this.data.autoLogin) {
            wx.removeStorageSync('rememberedPhone');
            wx.removeStorageSync('rememberedPassword');
          }
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success',
            duration: 1500,
            success: () => {
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/login/login'
                });
              }, 1500);
            }
          });
        }
      }
    });
  },

  // 返回上一页
  onBack: function() {
    wx.navigateBack();
  }
})